package com.yanq.liancar.activity;
/*
 *  @创建者     liyan
 *  @创建时间   2018/11/5  9:41
 *  @描述       ${TODO}
 */


import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.github.gcacace.signaturepad.views.SignaturePad;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.vondear.rxtool.RxActivityTool;
import com.vondear.rxtool.view.RxToast;
import com.yanq.liancar.R;
import com.yanq.liancar.activity.base.BaseToolbarActivity;
import com.yanq.liancar.bean.BaseBean;
import com.yanq.liancar.bean.CustomerSignatureBean;
import com.yanq.liancar.url.Config;
import com.yanq.liancar.util.LogUtils;
import com.yanq.liancar.util.Utils;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class CustomerSignatureActivity extends BaseToolbarActivity {
    @BindView(R.id.signaturePad)
    SignaturePad signaturePad;
    @BindView(R.id.tv_clear)
    Button tvClear;
    @BindView(R.id.tv_sure)
    Button tvSure;
    private int shopID;
    private File mFile;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.avtivity_customersignature);
        ButterKnife.bind(this);
        Utils.setStatusBar(this, true, false);
        shopID = getIntent().getIntExtra("shopID", 0);
        LogUtils.e("shopID", shopID + "");
        mTvTitle.setText("客户签字");

        signaturePad.setOnSignedListener(new SignaturePad.OnSignedListener() {
            @Override
            public void onStartSigning() {
                //屏幕绘图开始
            }

            @Override
            public void onSigned() {
                //手指离开屏幕时
                tvClear.setEnabled(true);
                tvSure.setEnabled(true);
            }

            @Override
            public void onClear() {
                //屏幕绘图被清除
                tvClear.setEnabled(false);
                tvSure.setEnabled(false);
            }
        });

    }

    @OnClick({R.id.tv_clear, R.id.tv_sure})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_clear:
                signaturePad.clear();
                break;
            case R.id.tv_sure:
                //signaturePad.getSignatureBitmap();
                saveBitmapFile(signaturePad.getSignatureBitmap());
                break;
        }
    }

    //图片上传
    private void initOkGo(File file) {
        OkGo.<String>post(Config.s + "llc/api/RoadRescueController/insertCustomerSignature")
                .tag(this)
                .params("customerSignaturePhoto", file)
                .params("id", shopID)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Gson gson = new Gson();
                        CustomerSignatureBean bean = gson.fromJson(response.body(), CustomerSignatureBean.class);
                        if (bean.isIsSuccess()) {
                            RxToast.success(bean.getOutputMessage());
                            Bundle bundle=new Bundle();
                            bundle.putInt("shopId",shopID);
                            RxActivityTool.skipActivity(mContext,SmallBRescueOrderActivity.class,bundle );
                        } else {
                            RxToast.error(bean.getOutputMessage());
                        }
                    }
                });
    }

    //bitmap转file
    public void saveBitmapFile(Bitmap bitmap) {
        //File file = new File("/mnt/sdcard/pic/01.jpg");//将要保存图片的路径
        mFile = new File(this.getExternalFilesDir("pic").getAbsolutePath() + "/" + System.currentTimeMillis() + ".jpg");//将要保存图片的路径
        LogUtils.e("图片路径：", this.getExternalFilesDir("pic").getAbsolutePath() + "/" + System.currentTimeMillis() + ".jpg");
        try {
            BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(mFile));
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bufferedOutputStream);
            bufferedOutputStream.flush();
            bufferedOutputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        initOkGo(mFile);
    }
}
